package com.beuth.taskList.activities;

import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

import com.beuth.taskList.R;
import com.beuth.taskList.activities.tools.DataIO;
import com.beuth.taskList.model.Priority;
import com.beuth.taskList.model.Task;
import com.beuth.taskList.model.TaskModel;
import com.beuth.taskList.model.TaskType;

public class TaskCreateOrEdit extends Activity {
	
	EditText editTextName;
	Spinner spinnerPriority;
	Spinner spinnerTaskType;
	DatePicker datePickerClosingDate;
	
	private DataIO dataIO = new DataIO();

	

	private TaskModel model = TaskModel.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_create_or_edit);
        
        editTextName = (EditText) findViewById(R.id.editText_create_task_name);
        spinnerPriority = (Spinner) findViewById(R.id.spinner_create_task_priority);
        spinnerTaskType = (Spinner) findViewById(R.id.spinner_create_task_taskType);
        datePickerClosingDate = (DatePicker) findViewById(R.id.datePicker_create_task_closingDate);
        
        spinnerPriority.setAdapter(new ArrayAdapter<Priority>(this, android.R.layout.simple_dropdown_item_1line, Priority.values()));
        spinnerTaskType.setAdapter(new ArrayAdapter<TaskType>(this, android.R.layout.simple_dropdown_item_1line, TaskType.values()));
        
        
    	Button button = (Button) findViewById(R.id.button_save_task);
    	button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(model.getCurrentEditTask() == null) {
					Task task = new Task();
					task = fillTaskFromView(task);
					model.addTask(task);	
				} else {
					fillTaskFromView(model.getCurrentEditTask());
				}
				model.setCurrentEditTask(null);
				Log.v("Model", ": Save Tasks");
				dataIO.save(model.getTasks());
				Intent i = new Intent(v.getContext(), TaskOverView.class);
		    	startActivity(i);
				
			}

			private Task fillTaskFromView(Task task) {
				task.setName(editTextName.getText().toString());
				task.setPriority((Priority)spinnerPriority.getSelectedItem());
				task.setTaskType((TaskType)spinnerTaskType.getSelectedItem());
				GregorianCalendar pickedDate = new GregorianCalendar(datePickerClosingDate.getYear(), datePickerClosingDate.getMonth(), datePickerClosingDate.getDayOfMonth());				
				task.setClosingDate(pickedDate);
				return task;
			}
		});
        
        
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	Bundle b = getIntent().getExtras();
    	if(b != null && b.get("EditTaskNumber") != null) {
    		Task selectedTask = model.getTasks().get((Integer) b.get("EditTaskNumber"));
    		model.setCurrentEditTask(selectedTask);
    		//Werte setzen
    		editTextName.setText(selectedTask.getName());
    		spinnerPriority.setSelection(selectedTask.getPriority().ordinal());
    		spinnerTaskType.setSelection(selectedTask.getTaskType().ordinal());
    		datePickerClosingDate.updateDate(selectedTask.getClosingDate().get(GregorianCalendar.YEAR) ,selectedTask.getClosingDate().get(GregorianCalendar.MONTH) , selectedTask.getClosingDate().get(GregorianCalendar.DAY_OF_MONTH));
    	}
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.task_over_view, menu);
        return true;
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.createTask:
    	  Intent i = new Intent(this, TaskCreateOrEdit.class);
    	  startActivity(i);        			
        break;
      case R.id.showTasks:
    	  i = new Intent(this, TaskOverView.class);
    	  startActivity(i); 
        break;

      default:
        break;
      }

      return true;
    } 
    
    
}
